/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.activity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.activity.vo.VBrandAgreementRenewalVo;
import com.ykht.proj.activity.vo.VBrandAgreementVo;
import com.ykht.proj.admin.api.entity.SysUser;
import com.ykht.proj.admin.api.feign.RemoteUserService;
import com.ykht.proj.common.core.constant.CommonConstants;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.activity.entity.VBrandAgreementRenewal;
import com.ykht.proj.activity.service.VBrandAgreementRenewalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 品牌协议续约表
 *
 * @author csg
 * @date 2021-05-18 14:25:31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/vbrandagreementrenewal")
@Api(value = "vbrandagreementrenewal", tags = "品牌协议续约表管理")
@Slf4j
public class VBrandAgreementRenewalController {

	private final VBrandAgreementRenewalService vBrandAgreementRenewalService;

	private final RemoteUserService remoteUserService;

	/**
	 * 协议续约分页查询
	 *
	 * @param page                     分页对象
	 * @param vBrandAgreementRenewalVo 品牌协议续约表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
//	@PreAuthorize("@pms.hasPermission('activity_vbrandagreementrenewal_view')")
	public R getVBrandAgreementRenewalPage(Page page, VBrandAgreementRenewalVo vBrandAgreementRenewalVo) {
		IPage<VBrandAgreementRenewalVo> dtoiPage = vBrandAgreementRenewalService.getBrandAgreementRenewalListPage(page, vBrandAgreementRenewalVo);
		List<VBrandAgreementRenewalVo> records = dtoiPage.getRecords();
		if (null == records || records.size() == 0) {
			return R.ok(null);
		}
		List<Long> longList = records.stream().map(VBrandAgreementRenewalVo::getOperationUser).collect(Collectors.toList());
		R<List<SysUser>> r = remoteUserService.getUserByIds(longList);
		if (r.getCode() == CommonConstants.SUCCESS && r.getData() != null && r.getData().size() > 0) {
			records.stream().forEach(itme -> {
				r.getData().stream().forEach(user -> {
					if (user.getUserId().equals(itme.getOperationUser().intValue())) itme.setUsername(user.getUsername());
				});
			});
		}
		dtoiPage.setRecords(records);
		return R.ok(dtoiPage);
	}


	/**
	 * 通过id查询品牌协议续约表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('activity_vbrandagreementrenewal_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(vBrandAgreementRenewalService.getById(id));
	}

	/**
	 * 新增品牌协议续约表
	 *
	 * @param vBrandAgreementRenewal 品牌协议续约表
	 * @return R
	 */
	@ApiOperation(value = "新增品牌协议续约表", notes = "新增品牌协议续约表")
	@SysLog("新增品牌协议续约表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('activity_vbrandagreementrenewal_add')")
	public R save(@RequestBody VBrandAgreementRenewal vBrandAgreementRenewal) {
		return R.ok(vBrandAgreementRenewalService.save(vBrandAgreementRenewal));
	}

	/**
	 * 修改品牌协议续约表
	 *
	 * @param vBrandAgreementRenewal 品牌协议续约表
	 * @return R
	 */
	@ApiOperation(value = "修改品牌协议续约表", notes = "修改品牌协议续约表")
	@SysLog("修改品牌协议续约表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('activity_vbrandagreementrenewal_edit')")
	public R updateById(@RequestBody VBrandAgreementRenewal vBrandAgreementRenewal) {
		return R.ok(vBrandAgreementRenewalService.updateById(vBrandAgreementRenewal));
	}

	/**
	 * 通过id删除品牌协议续约表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除品牌协议续约表", notes = "通过id删除品牌协议续约表")
	@SysLog("通过id删除品牌协议续约表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('activity_vbrandagreementrenewal_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(vBrandAgreementRenewalService.removeById(id));
	}


	@ApiOperation(value = "校验 协议编号")
	@GetMapping("/checkAgreementNumber")
	public R checkSjName(String agreementNumber) {
		log.info("->校验 协议编号{}", agreementNumber);
		VBrandAgreementRenewal check = new VBrandAgreementRenewal()
				.selectOne(Wrappers.lambdaQuery(VBrandAgreementRenewal.class)
						.eq(VBrandAgreementRenewal::getAgreRenewalCode, agreementNumber));
		return R.ok(check == null ? true : false);
	}


}
